package com.mioto.ld.communication.mqtt.protocol;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.mioto.ld.communication.mqtt.protocol.cache.DeviceInfoCache;
import com.mioto.ld.communication.mqtt.protocol.model.Message;
import com.mioto.ld.communication.mqtt.protocol.model.device.DevicePressTimePayload;
import com.mioto.ld.module.device.model.Device;
import com.mioto.ld.module.device.model.DevicePressTimeLog;
import com.mioto.ld.module.device.service.IDevicePressTimeLogService;
import com.mioto.ld.utils.SpringBeanUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executor;

/**
 * @author mioto-qinxj
 * @date 2024/6/13
 * @desc 按键时间上报处理
 */
@Slf4j
public class PressTimeHandleStrategy implements IReceiptHandleStrategy{

    @Override
    public void protocolHandler(Message message) throws Exception {
        log.info("----------------------设备 - {} 上报按键时长处理开始--------------------------", message.getSn());
        Device device = SpringBeanUtil.getBean(DeviceInfoCache.class).get(message.getSn());
        if (ObjectUtil.isNotNull(device)) {
            log.info("保存按键时长上报信息");
            DevicePressTimeLog devicePressTimeLog = new DevicePressTimeLog();
            devicePressTimeLog.setDeviceId(device.getId());
            DevicePressTimePayload payload = (DevicePressTimePayload) message.getMessageBody().getPayload();
            devicePressTimeLog.setReportDate(DateUtil.date(payload.getReportDate()));
            devicePressTimeLog.setPressTime(payload.getPressTime());
            SpringBeanUtil.getBean(IDevicePressTimeLogService.class).insert(devicePressTimeLog);
        }
        log.info("----------------------设备 - {} 上报按键时长处理结束--------------------------", message.getSn());
    }
}
